import { ref } from 'vue';
import type { Ref } from 'vue';
import type {PickPromise} from '@/typings/utils'

export const usePromise = <A extends () => Promise<any>,T extends PickPromise<ReturnType<A>>>(api: A):[() => Promise<void>,Ref<T | undefined>,Ref<boolean>] => {
	const status = ref(false);
	const requestData = ref<T | undefined>(undefined);
	const handleRequest = async () =>{
		status.value = true;
		try{
			const res = await api()
			requestData.value = res
		}catch(e){
			//TODO handle the exception
		}
		status.value = false;
	}
		
	return [handleRequest, requestData, status];
};
